package command

import (
	"encoding/json"
	"fmt"

	"github.com/gocolly/colly/v2"
)

type Data struct {
	Httpstatus int `json:"httpstatus"`
	Data       struct {
		Result []string `json:"result"`
		Flag   string   `json:"flag"`
		Map    struct{} `json:"map"`
	} `json:"data"`
	Messages string `json:"messages"`
	Status   bool   `json:"status"`
}

var data Data

func Start(u string) {
	c1 := colly.NewCollector(
	//colly.UserAgent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"),
	)
	//c1.UserAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
	//c1.Request()
	c1.OnRequest(func(req *colly.Request) {
		req.Headers.Add("cookie", "_jc_save_fromStation=%u5317%u4EAC%2CBJP; _jc_save_toStation=%u4E0A%u6D77%2CSHH; _jc_save_fromDate=2023-06-04; _jc_save_toDate=2023-06-03; _jc_save_wfdc_flag=dc")
		req.Headers.Add("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36")
		req.Headers.Add("Referer", "https://kyfw.12306.cn/otn/leftTicket/init?linktypeid=dc")
	})
	c1.OnResponse(func(res *colly.Response) {
		err := json.Unmarshal(res.Body, &data)
		if err != nil {
			return
		}
		//fmt.Println(string(res.Body))
	})
	c1.OnError(func(res *colly.Response, err error) {
		fmt.Println(err)
	})
	err := c1.Visit(u)
	if err != nil {
		return
	}

	c1.Wait()
	Jx()
}

//https://kyfw.12306.cn/otn/leftTicket/query
//?leftTicketDTO.train_date=2023-06-04&leftTicketDTO.from_station=BJP&leftTicketDTO.to_station=SHH&purpose_codes=ADULT
